<?php

namespace catchAdmin\model\wechat;

use catcher\library\client\Http;
use EasyWeChat\Factory;

class FuWuHaoApp extends \EasyWeChat\OfficialAccount\Application
{

    private $app_id = 'wxbe6d52d49560cc65';
    private $secret = 'wxbe6d52d49560cc65';
    private $app    = null;


    public function __construct()
    {
        $config = [
            'app_id'        => $this->app_id,
            'secret'        => $this->secret,
            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',//
//            'log' => [
//                'level' => 'debug',
//                'file' => __DIR__.'/wechat.log',
//            ],
        ];

        $this->app = Factory::officialAccount($config);

        return $this->app;
    }


    public function getUserByCode($code)
    {
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
        $url .= '?appid=' . $this->app_id;
        $url .= '&secret=' . $this->secret;
        $url .= '&code=' . $code;
        $url .= '&grant_type=authorization_code';

        return (new Http())->get($url);
    }


    public function getUserByOpenid($openid)
    {
        return $this->app->user->get($openid);
    }


}
